package com.bdy.voice.ui;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.Nullable;
import com.bdy.voice.databinding.ModuleBdyvoiceXukunActivityVoicecompoundBinding;
import com.bdy.voice.util.VoicecompoundTool;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.libbasecoreui.mvp.BaseActivity;

public class VoicecompoundActivity extends BaseActivity {
    private ModuleBdyvoiceXukunActivityVoicecompoundBinding binding;
    private String content;
    //语音合成sdk初始化的参数,appId不需要加密
    private String appId = "27568421";
    //封装好的语音合成类
    private VoicecompoundTool voicecompoundTool;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ModuleBdyvoiceXukunActivityVoicecompoundBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        voicecompoundTool = new VoicecompoundTool(this);
        //第二个参数由运营配置好，通过密钥获取appkey和secretKey
        voicecompoundTool.initbdyKey(appId,"com.nsxf.miwu_voice");
        initView();

    }



    private void initView() {
        binding.imageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content = binding.etWord.getText().toString();
                if (content == null||content.isEmpty()){
                    ToastUtils.showShort("请输入文字内容！");
                    return;
                }
                speak(content);
            }
        });

        binding.btnPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pause();
            }
        });

        binding.btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                restart();
            }
        });


    }

    private void speak(String content) {
        voicecompoundTool.speak(content);
    }

    private void pause(){
        voicecompoundTool.pause();
    }

    private void restart(){
        voicecompoundTool.restart();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        voicecompoundTool.release();
    }
}
